Page({
  data: {
    boat: {
      id: '',
      boatNumber: '',
      ownerName: '',
      idCard: '',
      phone: ''
    }
  },

  onLoad(options) {
    if (options.id) {
      this.loadBoatInfo(options.id);
    }
  },

  loadBoatInfo(id) {
    wx.request({
      url: `https://api.example.com/boats/${id}`,
      method: 'GET',
      success: (res) => {
        if (res.statusCode === 200) {
          this.setData({
            boat: res.data
          });
        } else {
          wx.showToast({
            title: '获取渔船信息失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      }
    });
  },

  onBoatNumberChange(e) {
    this.setData({
      'boat.boatNumber': e.detail.value
    });
  },

  onOwnerNameChange(e) {
    this.setData({
      'boat.ownerName': e.detail.value
    });
  },

  onIdCardChange(e) {
    this.setData({
      'boat.idCard': e.detail.value
    });
  },

  onPhoneChange(e) {
    this.setData({
      'boat.phone': e.detail.value
    });
  },

  submit() {
    const { boat } = this.data;
    if (!boat.boatNumber || !boat.ownerName || !boat.idCard || !boat.phone) {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none'
      });
      return;
    }

    wx.request({
      url: `https://api.example.com/boats/${boat.id}`,
      method: 'PUT',
      data: boat,
      success: (res) => {
        if (res.statusCode === 200) {
          wx.showToast({
            title: '保存成功',
            icon: 'success'
          });
          setTimeout(() => {
            wx.navigateBack();
          }, 1500);
        } else {
          wx.showToast({
            title: '保存失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      }
    });
  },

  goBack() {
    wx.navigateBack();
  }
}); 